<?php
/*
	申请成为代理商
	如果已申请，但尚未审核通过，则页面显示请耐心等待管理员审核！
	如果已申请，且审核通过，则显示审核通过，并提供按钮前往代理中心
	如果审核不通过，则显示审核不通过原因
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
if($_RQ['op']=='main'){
	$member=$this->model('zhaoshang')->checkmember();
	$setting=$this->plugin_setting('page');
	$return['setting']=$setting;
	$apply=$this->plugin_setting('apply');
	$return['apply']=$apply;
	$return['member']=$member?:false;
	$return['item']=model('core/member')->detail($_SESSION['uid'],array('nickname','avatar','realname','mobile','wechat'));
	$return['item']['diyformfields']=$setting['diyformfields']?:array();
	if($this->setting['become']==4){
		$tips='自购总部商品金额，满'.$this->setting['money'].'元，自动开通！';
	}elseif($this->setting['become']==5){
		$tips='自购指定总部商品，将自动开通！';
		$become_goods=$this->setting['become_goods'];
		foreach($become_goods as &$bgv){
			$goods=pdo_get('jy_weishop_goods',array('id'=>$bgv['id']),array('id','title','image','price','active_plugin','active_id','salenum','originalprice','usermaxbuy','unit'));
			$bgv=array_merge($goods,$bgv);
		}
		$return['become_goods']=$become_goods;
	}
	$return['tips']=$tips;
	set_html('title','申请成为'.$setting['agent_title']);
	exi($return);
}
if($_RQ['op']=='apply'){
	$post=get_postdata();
	if(!$post['realname']){
		exi('请输入真实姓名！','error');
	}
	if(!$post['agree']){
		exi('请先阅读协议！','error');
	}
	if(!$post['mobile']){
		exi('请输入联系电话！','error');
	}
	if(strlen($post['mobile'])!=11){
		exi('请填写正确的手机号','error');
	}
	$data =array(
		'realname'=>$post['realname'],
		'mobile'=>$post['mobile'],
		'status'=>0,
	);
	//代理地区整理
	if(!$post['area'] || !$post['area']['province']){
		exi('请选择所属地区！','error');
	}
	$data['code']=model('jy_merch/model')->get_area_code($post['area']);
	$this->model('zhaoshang')->register($data);
	$member_update=array(
		'realname'=>trim($post['realname']),
		'mobile'=>trim($post['mobile']),
		'diyformdata'=>ijson_encode($post['diyformfields']),
	);
	if($post['wechat']){
		$member_update['wechat']=trim($post['wechat']);
	}
	model('core/member')->member_update($_SESSION['uid'],$member_update);	
	exi('已申请,请等待审核','','jy_mzhaodian/apply/');
}